package com.emip.view;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;

import com.emip.pojo.Data;
import com.emip.pojo.Report;
import com.emip.util.Util;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;


@Component
public class ReportPdfView extends AbstractIText5PdfView {

	@Override
    protected void buildPdfDocument(Map<String, Object> model,
            Document document, PdfWriter writer, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
		
		Report report = (Report) model.get("report");
		if (report == null) return;
		List<Map<String, Object>> bodys = report.getBodys();
		List<Map<String, Object>> footers = report.getFooters();
		
		
		List<String> headerNames = report.getColNames();
		List<String> indexes = report.getIndexes();
		
		Map<Integer, String> position = new HashMap<Integer, String>();
		
		//pdf
		//header
        Paragraph header = new Paragraph(new Chunk("报表数据",
                getChineseFont(20)));
        header.setSpacingAfter(36);
        header.setAlignment(Element.ALIGN_CENTER);
        document.add(header);
//        document.add(new Paragraph("测试",getChineseFont(12)));
        
        //绘制表格
        PdfPTable table = new PdfPTable(indexes.size() + 1);
        
        Font headerFont = getChineseFont(10);
        table.setWidthPercentage(100);
        //初始化表头
        table.addCell(new Phrase(new Chunk("日期", headerFont)));
        for (int i = 0; i < headerNames.size(); i++) {
        	table.addCell(new Phrase(new Chunk(headerNames.get(i), headerFont)));
        	position.put(i+1, indexes.get(i));
        }
        position.put(0, "time");
        
        //bodys
        for (int i = 0; i < bodys.size(); i++) {
			Map<String, Object> dataRow = bodys.get(i);
			for (int j = 0; j <= headerNames.size(); j++) {
				Object obj = dataRow.get(position.get(j));
				if (obj instanceof Data) {
					Data data = (Data) obj;
					String txt = Util.Float2String(data.getValue());
					table.addCell(new Phrase(new Chunk(txt, headerFont)));
					//TODO set flag
					if (data.getFlag() != null) {
						
					}
				} else {
					String txt = obj == null ? "" : obj.toString();
					table.addCell(new Phrase(new Chunk(txt, headerFont)));
				}
			}
        }
        
        //footers
        for (int i = 0; i < footers.size(); i++) {
			Map<String, Object> dataRow = footers.get(i);
			for (int j = 0; j <= headerNames.size(); j++) {
				Object obj = dataRow.get(position.get(j));
				String value = obj == null ? "" : obj.toString();
				if (j == 0) {
					table.addCell(new Phrase(new Chunk(value, headerFont)));
				} else 
					table.addCell(new Phrase(new Chunk(value, headerFont)));
			}
        }
        
        document.add(table);
    }

    private static final Font getChineseFont(float size) {
        Font FontChinese = null;
        try {
            BaseFont bfChinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            FontChinese = new Font(bfChinese, size, Font.NORMAL);
        } catch (DocumentException de) {
            System.err.println(de.getMessage());
        } catch (IOException ioe) {
            System.err.println(ioe.getMessage());
        }
        return FontChinese;
    }

}
